package interaction;

import java.util.HashMap;

import processing.core.PFont;
import processing.core.PVector;
import interaction.audio.AudioProcessor;
import interaction.data.DataChangeListener;
import interaction.input.Component;
import interaction.input.ComponentSwitcher;
import interaction.input.concrete.BeatImagePromptComponent;
import interaction.input.concrete.CalibrationGameComponent;
import interaction.input.concrete.GameComponent;
import interaction.input.concrete.ImagePromptComponent;
import interaction.input.concrete.ScoreComponent;
import interaction.input.concrete.TestComponent;
import interaction.input.concrete.IntroComponent;
import interaction.input.concrete.SlapSignatureComponent;
import interaction.input.concrete.TapSignatureComponent;
import interaction.sampling.SampleSet;

public class Bootstrap {
	private ImagePromptComponent introComponent;
	private ImagePromptComponent preTapComponent;
	private CalibrationGameComponent.TapCalibration tapComponent;
	private ImagePromptComponent preSlapComponent;
	private CalibrationGameComponent.SlapCalibration slapComponent;
	private BeatImagePromptComponent preGameComponent;
	private GameComponent gameComponent;
	private ScoreComponent scoreComponent;
	
	private ComponentSwitcher<String> switcher;
	
	public void init(App a)
	{
		ServiceLocator.setApplet(a);
		
		a.size(800, 600);
		a.setAudioController(new AudioProcessor(a));
		
		introComponent = new ImagePromptComponent(new PVector(), a.loadImage("interaction/game/images/scenes/intro.png"));
		preTapComponent = new ImagePromptComponent(new PVector(), a.loadImage("interaction/game/images/scenes/pre_tap_calibration.png"));
		tapComponent = new CalibrationGameComponent.TapCalibration(new PVector(), a.getSizeVector(), a); 
		preSlapComponent = new ImagePromptComponent(new PVector(), a.loadImage("interaction/game/images/scenes/pre_slap_calibration.png"));
		slapComponent = new CalibrationGameComponent.SlapCalibration(new PVector(), a.getSizeVector(), a);
		preGameComponent = new BeatImagePromptComponent(new PVector(), a.loadImage("interaction/game/images/scenes/pre_game.png"));
		gameComponent = new GameComponent(new PVector(), a.getSizeVector(), a);
		scoreComponent = new ScoreComponent(new PVector(), a.loadImage("interaction/game/images/scenes/scores.png"), a.loadFont("interaction/game/data/Palatino-Bold-40.vlw"));
		
		switcher = new ComponentSwitcher<String>(new HashMap<String, Component>() {{
			put("intro", introComponent);
			put("preTap", preTapComponent);
			put("tap", tapComponent);
			put("preSlap", preSlapComponent);
			put("slap", slapComponent);
			put("preGame", preGameComponent);
			put("game", gameComponent);
			put("score", scoreComponent);
		}}, "intro");
		
		a.add(switcher);
		
		introComponent.attachDataChangeListener(new DataChangeListener<Boolean>() {
			public void dataChanged(Boolean from, Boolean to) {
				if (to) {
					switcher.setSelectedKey("preTap");
				}
			}
		});
		
		preTapComponent.attachDataChangeListener(new DataChangeListener<Boolean>() {
			public void dataChanged(Boolean from, Boolean to) {
				if (to) {
					tapComponent.init();
					switcher.setSelectedKey("tap");
				}
			}
		});
		
		tapComponent.attachDataChangeListener(new DataChangeListener<SampleSet>() {
			public void dataChanged(SampleSet from, SampleSet to) {
				if (to != null) {
					ServiceLocator.setTapSamples(to);
					switcher.setSelectedKey("preSlap");
				}
			}
		});
		
		preSlapComponent.attachDataChangeListener(new DataChangeListener<Boolean>() {
			public void dataChanged(Boolean from, Boolean to) {
				if (to) {
					slapComponent.init();
					switcher.setSelectedKey("slap");
				}
			}
		});
		
		slapComponent.attachDataChangeListener(new DataChangeListener<SampleSet>() {
			public void dataChanged(SampleSet from, SampleSet to) {
				if (to != null) {
					ServiceLocator.setSlapSamples(to);
					switcher.setSelectedKey("preGame");
				}
			}
		});
		
		preGameComponent.attachDataChangeListener(new DataChangeListener<String>() {
			public void dataChanged(String from, String to) {
				if ("slap".equals(to)) {
					gameComponent.init();
					switcher.setSelectedKey("game");
				}
			}
		});
		
		gameComponent.attachDataChangeListener(new DataChangeListener<Integer>() {
			public void dataChanged(Integer from, Integer to) {
				scoreComponent.setScore(to);
				switcher.setSelectedKey("score");
			}
		});
		
		scoreComponent.attachDataChangeListener(new DataChangeListener<String>() {
			public void dataChanged(String from, String to) {
				if ("slap".equals(to)) {
					gameComponent.init();
					switcher.setSelectedKey("game");
				} else if ("click".equals(to)) {
					switcher.setSelectedKey("preTap");
				}
			}
		});
	}
}
